package com.kmfish.smsbackup;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class SmsItemAdapter extends ArrayAdapter<MySmsMessage> implements OnClickListener {
	
	private final LayoutInflater mInflater = LayoutInflater.from(getContext());
	public final Map<Integer, Boolean> checkedItems = new HashMap<Integer, Boolean>();
	private final Map<String, String> contactNameMap = new HashMap<String, String>();
	private final StringBuilder builder = new StringBuilder();
	public int mode = NORMAL_MODE;
	public static final int NORMAL_MODE = 0;
	public static final int THREAD_MODE = 1;
	
	public SmsItemAdapter(Context context) {
		super(context, 0);
	}

	private static class ViewHolder {
		TextView dateTime;
		TextView contact;
		TextView smsBody;
		CheckBox checkBox;
	}
	
	@Override
	public final View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView)
		{
			convertView = mInflater.inflate(
					R.layout.list_sms_item, null);
			
			holder = new ViewHolder();
			holder.dateTime = (TextView) convertView.findViewById(R.id.date);
			holder.contact = (TextView) convertView.findViewById(R.id.contact);
			holder.smsBody = (TextView) convertView.findViewById(R.id.body);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		MySmsMessage sms = getItem(position);
		String dateText = Util.DatetoString(new Date(sms.timestamp),
				"yyyy-MM-dd HH:mm:ss");
		holder.dateTime.setText(dateText);
		holder.checkBox.setChecked(checkedItems.containsKey(position)
				&& checkedItems.get(position));
		holder.checkBox.setTag(position);
		holder.checkBox.setOnClickListener(this);
		holder.smsBody.setText(sms.body);
		if (mode == NORMAL_MODE) {
			holder.contact.setText(createTypeStr(sms.type, createContactStr(sms.address)));
		}else if (mode == THREAD_MODE) {
			holder.contact.setText(createContactStr(sms.address, sms.count));
		}
		return convertView;
	}

	@Override
	public void onClick(View v) {
		int position = (Integer)v.getTag();
		boolean isChecked = checkedItems.containsKey(position)
		&& checkedItems.get(position);
		checkedItems.put(position, !isChecked);
	}
	
	public static final String createTypeStr(int type, String contactStr) {
		StringBuilder builder = new StringBuilder();
		builder.append(contactStr);
		if (type == MySmsMessage.MESSAGE_TYPE_RECEIVE) {
			builder.insert(0, "From: ");
		}else if (type == MySmsMessage.MESSAGE_TYPE_SENT) {
			builder.insert(0, "To: ");
		}
		return builder.toString();
	}
	
	private String getContactNameByPhoneNumber(String number)
	{
		if (number.startsWith(SMS_PHONE_NUMBER_PREFIX)) {
			number = number.substring(SMS_PHONE_NUMBER_PREFIX.length());
		}
		MyContact contact = ContactsUtil.getContactDetails(getContext(), number);
		if (null != contact) {
			return contact.name;
		}else {
			return number;
		}
	}
	
	public static final String SMS_PHONE_NUMBER_PREFIX = "+86";
	public final String createContactStr(String phoneNumber)
	{
		return createContactStr(phoneNumber, 0);
	}
	
	public String createContactStr(String phoneNumber, long msgCount)
	{
		return createContactStr(phoneNumber, msgCount, true);
	}
	
	public String createContactStr(String phoneNumber, long msgCount, boolean isUsedBuff)
	{
		if (contactNameMap.containsKey(phoneNumber) && isUsedBuff) {
			return contactNameMap.get(phoneNumber);
		}
		
		builder.setLength(0);
		builder.append(getContactNameByPhoneNumber(phoneNumber));
		builder.append("(");
		if (msgCount <= 0) {
			builder.append(phoneNumber);
		}else {
			builder.append(msgCount);
		}
		builder.append(")");
		contactNameMap.put(phoneNumber, builder.toString());
		return builder.toString();
	}
	
	public MySmsMessage[] getSmsMessages(int position)
	{
		MySmsMessage sms = getItem(position);
		if (null != sms) {
			if (mode == THREAD_MODE) {
				return SmsUtil.getSmsMessagesByThreadID(getContext(), sms.threadId);
			}
		}
		return null;
	}
	
	
	
}